package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.dto.BrowseCategory;
import com.guigu.qingcheng.pojo.dto.OrderBrowse;
import com.guigu.qingcheng.pojo.dto.ReturnOrderDto;
import com.guigu.qingcheng.pojo.vo.CategoryReportVo;
import com.guigu.qingcheng.service.StatisService;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/statis")
@Api(tags = "StatisController")
public class StatisController {

    @Autowired
    private StatisService statisService;

    @ApiOperation(value = "查询多天数据统计的交易额")
    @PostMapping("/browseAndOrder")
    public R queryByBrowseAndOrder(@RequestBody CategoryReportVo categoryReportVo) {
        List<BrowseCategory> tbCategoryReports = statisService.queryByBrowseOrder(categoryReportVo);
        return R.ok().data("browseAndOrder",tbCategoryReports);
    }

    @ApiOperation(value = "查询多天数据统计的总交易以及退款")
    @PostMapping("/findByOrderBrowse")
    public R findByOrderBrowse(@RequestBody CategoryReportVo categoryReportVo) {
        List<OrderBrowse> tbCategoryReports = statisService.findAllOrderAndBrowse(categoryReportVo);
        List<ReturnOrderDto> tbCategoryReports2 = statisService.findAllReturnOrder(categoryReportVo);
        Map map = new HashMap();
        map.put("money",tbCategoryReports.get(0).getMoney());
        map.put("numPeople",tbCategoryReports.get(0).getNumPeople());
        map.put("numOrder",tbCategoryReports.get(0).getNumOrder());
        map.put("num",tbCategoryReports.get(0).getNum());
        map.put("numVisitors",tbCategoryReports.get(0).getNumVisitors());
        map.put("numValidOrders",tbCategoryReports.get(0).getNumOrder()-tbCategoryReports2.get(0).getNumOrder());
        if (tbCategoryReports2.get(0).getMoney()==null){
            tbCategoryReports2.get(0).setMoney(0);
        }
        map.put("refundAmount",tbCategoryReports2.get(0).getMoney());
        return R.ok().data("orderInfo",map);
    }


    @ApiOperation(value = "查询多天数据统计的下单app")
    @PostMapping("/findOrderSourceType")
    public R findOrderSourceType(@RequestBody CategoryReportVo categoryReportVo) {
        List<Map> tbCategoryReports = statisService.findOrderSourceType(categoryReportVo);
        return R.ok().data("tbOrder",tbCategoryReports);
    }
}
